package cate.game.role.res.guild.po.my.skill;

import cate.common.table.d.GDGuild;
import cate.common.table.d.GDHero;
import cate.common.table.guild.GuildSkillRow;
import cate.game.attr.FightAttr;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.res.guild.po.my.skill.msg.GuildSkillChangeResp;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.*;

public class GuildNormalSkill extends RoleDependent implements GuildSkillOperation {

    @NoteField(value = "不同职业下的技能培养", detail = "Map<Job,order>")
    public Map<Byte, Integer> jobSkillStatus;
    @NoteField(value = "不同职业下重置记录", detail = "Map<Job,haveBackedOrNot>")
    public List<Byte> jobSkillBakeRecord;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (jobSkillStatus == null) {
            jobSkillStatus = new HashMap<>();
            for (byte job : GDHero.Job.ALL) {
                jobSkillStatus.put(job, GDGuild.Skill.NORMAL_SKILL_ORIGIN_LEVEL);
            }
            jobSkillStatus.entrySet().removeIf(entry -> entry.getKey() == GDHero.Job.NULL);
        }
        if (jobSkillBakeRecord == null) {
            jobSkillBakeRecord = new LinkedList<>();
        }
    }

    @Override
    public boolean match(byte type) {
        return type == GDGuild.Skill.NORMAL_SKILL_TYPE;
    }

    @Override
    public void upgrade(byte type, int order, byte job, MyGuildSkillPO myGuildSkillPO) {
        if (job == GDHero.Job.NULL || !GDHero.Job.valid(job)) {
            role.getGame().notice.message(role, "职业不存在");
            return;
        }
        order = jobSkillStatus.get(job);
        GuildSkillRow currentRow = role.getGame().table.guild.skill.onGetRow(type, job, order);
        GuildSkillRow nextRow = role.getGame().table.guild.skill.onGetRow(type, job, order + 1);
        if (nextRow == null) {
            role.getGame().notice.message(role, "当前版本已经满级");
            return;
        }
        if (jobSkillStatus.values().stream().anyMatch(jobOrder -> nextRow.floorRequire > role.getGame().table.guild.skill.onGetRow(type, job, jobOrder).level)) {
            role.getGame().notice.message(role, "继续点亮技能需要所有职业技能达到" + nextRow.floorRequire + "层");
            return;
        }

        EcResult<MixResItem> consumeRes = new MixRes(currentRow.costStr)
                .consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            return;
        }

        jobSkillStatus.put(job, order + 1);
        noticeUpdate(type, job, order + 1, nextRow.level);
        role.getHistory().action.guildGeneralSkill(job, nextRow.level);
        role.getRes().guild.active.addActive(GDGuild.activeTask.LIGHT_GUILD_SKILL);
    }

    private void noticeUpdate(byte type, byte job, int order, int level) {
        role.sendNow(new GuildSkillChangeResp(type, job, order, level));
    }


    @Override
    public void back(byte type, byte job, MyGuildSkillPO myGuildSkillPO) {
        int order = jobSkillStatus.get(job);
        GuildSkillRow currentRow = role.getGame().table.guild.skill.onGetRow(type, job, order);
//        if (!jobSkillBakeRecord.contains(job)) {
//            EcResult<MixResItem> consumeRes = new MixRes(GDGuild.Skill.FIRST_COST_FOR_NORMAL_SKILL_BACK)
//                    .consume(role, null);
//            if (!consumeRes.ok()) {
//                role.getGame().notice.tipResItem(role, consumeRes.data);
//                return;
//            }
//            jobSkillBakeRecord.add(job);
//        } else {
//            EcResult<MixResItem> consumeRes = new MixRes(currentRow.backCostStr)
//                    .consume(role, null);
//            if (!consumeRes.ok()) {
//                role.getGame().notice.tipResItem(role, consumeRes.data);
//                return;
//            }
//        }
        EcResult<MixResItem> consumeRes = new MixRes(GDGuild.Skill.COST_FOR_NORMAL_SKILL_BACK)
                .consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            return;
        }

        MixRes reward = new MixRes(currentRow.backRewardStr);
        new MixResAdder().setRes(reward)
                .setDialogReward(true)
                .exe(role);
        jobSkillStatus.put(job, GDGuild.Skill.NORMAL_SKILL_ORIGIN_LEVEL);
    }

    @Override
    public FightAttr onGetAttr(byte type, byte job, MyGuildSkillPO myGuildSkillPO) {
        int order = jobSkillStatus.get(job);
        GuildSkillRow currentRow = role.getGame().table.guild.skill.onGetRow(type, job, order);
        if (currentRow == null) {
            return new FightAttr();
        }
        return new FightAttr(currentRow.attrStr);
    }

    @Override
    public List<Integer> onGetSkill(byte type, byte job, MyGuildSkillPO myGuildSkillPO) {
        return new ArrayList<>();
    }

    @Override
    public void gmHighestLevel() {
        int levelMax = role.getGame().table.guild.skill.getList().stream().mapToInt(row -> row.order).max().orElse(0);
        for (byte job : GDHero.Job.ALL) {
            if (job == GDHero.Job.NULL) {
                continue;
            }
            jobSkillStatus.put(job, levelMax);
        }
    }

    @Override
    public void gmRebuildAll() {
        jobSkillStatus = new HashMap<>();
        for (byte job : GDHero.Job.ALL) {
            jobSkillStatus.put(job, GDGuild.Skill.NORMAL_SKILL_ORIGIN_LEVEL);
        }
        jobSkillStatus.entrySet().removeIf(entry -> entry.getKey() == GDHero.Job.NULL);
    }

    public int onGetJobSkillFloor(byte job) {
        GuildSkillRow row = role.getGame().table.guild.skill.onGetRow(GDGuild.Skill.NORMAL_SKILL_TYPE, job, jobSkillStatus.get(job));
        if (row == null) {
            return -1;
        }
        return row.level;
    }

}
